<?

  error_reporting(E_ALL);
  ini_set('display_errors', 1);

if (empty($_COOKIE['user'])) {
  header('Location: index.php');
}

include_once 'controller.inc.php';
$db_link = db_connect();
$user = db_getUserByEmail($_COOKIE['user']);
$user_roles = db_getRolesForUser($user);
$all_events = db_getAllEvents();
db_disconnect($db_link);

$other_events = array();
$event_ids = array();
$idx = 1;
foreach ($user_roles as $role) {
  $event_ids[$idx++] = $role->getEvent()->getID();
}

$idx = 1;
foreach ($all_events as $event) {
  if (!in_array($event->getID(), $event_ids)) {
    $other_events[$idx++] = $event;
  }
}

include("header.inc.php");

?>

<?
if (isset($_GET['success'])) {
  $success_info = $_GET['success'];
  $success_info = explode('_', $success_info);
  echo '<p class="success">'.$success_info[0].' '.$success_info[1].' successfully.</p>';
}

if (isset($_GET['failure'])) {
  if ($_GET['failure'] == 'email') {
    echo '<p class="error">An account with this email address already exists.</p>';
  }
  else if ($_GET['failure'] == 'auth') {
    echo '<p class="error">Unauthorized action.</p>';
  }
}
?>

<?
echo '<h2>'.$user->getName()."'s Events:</h2>";
?>

<ul>
<?

if (empty($user_roles)) {
  echo '<li>You are not registered for any events.</li>';
}

foreach ($user_roles as $role) {
  $event = $role->getEvent();
  echo '<li><a href="view_event.php?id='.$event->getID().'">'.$event->getName().'</a> - '.$role->getName().'</li>';
}

?>
</ul>

<h2>Join New Event:</h2>
<ul>
<?

if (empty($other_events)) {
  echo '<li>There are no more events for you to join!</li>';
}

foreach ($other_events as $event) {
  echo '<li><a href="view_event.php?id='.$event->getID().'">'.$event->getName().'</a></li>';
}

?>
</ul>

<p>
  <a href="new_event.php">Add an Event</a> - <a href="edit_user.php">Edit Account Info</a>
</p>

<form name="logout" action="logout.php" method="post">
  <p><input type="submit" name="action" value="Log Out"></p>
</form>

<?

include("footer.inc.php");

?>
